<?php
require "../common/user_online.php";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>EbookAZ</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<?php require "../components/head.php" ?>
</head>
<body onload="MakeCurrentPage(1)">
<div id="container">
	<?php require "../components/user_menu.php" ?>
	<?php require "../components/user_header.php" ?>
	<div id="content">
		<?php require "../components/content_left.php"?>
		<div id="content_right">
		<div align='center'>			
			<h1 style='text-align:center'>Cập nhật thông tin ebook</h1>
			<?php
			
			//Kết nối csdl
			require "../common/db.inc";
			$conn = new mysqli($host, $username, $password, $dbname);	
			$conn->query("set names 'utf8'");		
			//check connection
			if (mysqli_connect_errno()) {
				printf("Connect failed: %s\n", mysqli_connect_error());
				exit();
			}
		
			$ebookid = $_REQUEST["id"];	
			$sql = "call sp_GetEbookById($ebookid);";
			$result = $conn->query($sql);
			
			if(mysqli_num_rows($result))
			{
				$row = $result->fetch_row();
				$id	= $row[0];
				$title = $row[1];
				$fileupload = $row[2];		
				$type = $row[3];
				$desc = $row[4];
				$author = $row[5];
				$publisher = $row[6];
				$pagenum = $row[7];
				$img = $row[8];
				$size = $row[9];
				$category = $row[16];
				$updatedate = $row[11];		
				
				if(isset($_REQUEST["errorImg"]))
				{
					if($_REQUEST["errorImg"] == "other")
						echo "<b style='color:#FF0000'>Ảnh minh họa bị lỗi. Vui lòng chọn ảnh khác</b><br>";
					else if($_REQUEST["errorImg"] == "size")
						echo "<b style='color:#FF0000'>Ảnh minh họa có kính thước quá lớn. Vui lòng chọn ảnh khác</b><br><br>";						
					
					$title = $_SESSION["edit_title"];
					$type = $_SESSION["edit_type"];
					$category = $_SESSION["edit_category"];
					$author = $_SESSION["edit_author"];
					$publisher = $_SESSION["edit_publisher"];
					$pagenum = $_SESSION["edit_pagenum"];
					$desc = $_SESSION["edit_desc"];
				}
			?>
			
			<form id="formUpLoad" name="formEditEbook" action="ebook_update.php" method="post" enctype="multipart/form-data">
			<input type="hidden" name="hdID" value="<?php echo $id;?>"/>
			<input type="hidden" name="hdImage" value="<?php echo $img;?>"/>
			<table>
			<tr>
			<td valign="top"><img src="<?php echo $image.$img;?>" width="200px" height="300px"/></td>
			<td valign="top">		
				<table cellspacing="4" style="text-align:left">					
					<tr valign="top">
						<td><b>Tiêu đề</b></td>
						<td><input name="txtTitle" type="text" style="width: 350px" value="<?php echo $title; ?>" onblur="checkTitle()"></input><br><b id='bTitle'></b></td>
						<td><b style="color:#8C0000">*</b></td>
					</tr>
					<tr valign="top">
						<td><b>Tác giả</b></td>
						<td><input name="txtAuthor" type="text" style="width: 350px"  value="<?php echo $author; ?>" onblur="checkAuthor()"/><br><b id='bAuthor'></b></td>
						<td><b style="color:#8C0000">*</b></td>
					</tr>				
					<tr valign="top">
						<td><b>Nhà xuất bản</b></td>
						<td><input name="txtPublisher" type="text" style="width: 350px" value="<?php echo $publisher; ?>"/><br><b id='bPublisher'></b></td>
						<td>&nbsp;</td>
					</tr>
				
					<tr valign="top">
						<td><b>Lĩnh vực</b></td>
						<td>
							<select name="cmbCat" style='width:170px' onblur="checkCategory()">
								<?php
								$conn = new mysqli($host, $username, $password, $dbname);	
								$conn->query("set names 'utf8'");
								
								$sql = "call sp_GetAllField()";
								$arrField = $conn->query($sql);
								while($field = $arrField->fetch_row())
								{							
									echo "<optgroup label='$field[1]'>";
									
									$conn = new mysqli($host, $username, $password, $dbname);	
									$conn->query("set names 'utf8'");
									
									$sql = "call sp_GetCategoryByField('$field[0]')";
									$arrCat = $conn->query($sql);
									while($cat = $arrCat->fetch_row())
									{	
										if($cat[0] == $category)
											echo "<option value='$cat[0]' selected='selected'>$cat[1]</option>";
										else						
											echo "<option value='$cat[0]'>$cat[1]</option>";
									}				
															
									echo "</option>";
								}
								?>	
							</select>				
							<br /><b id='bCat'></b>
						</td>
						<td><b style="color:#8C0000">*</b></td>
					</tr>
					
					<tr valign="top">
						<td><b>Loại ebook</b></td>
						<td>
							<?php
							require "../common/db.inc";
							$conn = new mysqli($host, $username, $password, $dbname);	
							$conn->query("set names 'utf8'");	
							
							echo "<select id='cmbType' name='cmbType' style='width:170px' value='$type'>";
								$strSQL = "call sp_GetAllEbookType()";
								$arrType = $conn->query($strSQL);
								while($row = $arrType->fetch_row())
								{							
									echo "<option value='$row[0]'>$row[1]</option>";
								}						
							echo "</select>";
							?>
						</td>
						<td>&nbsp;</td>
					</tr>			
					<tr valign="top">
						<td><b>Số trang</b></td>
						<td><input name="txtPageNum" type="text" size="5px"  value="<?php echo $pagenum; ?>" onblur="checkPageNum()"/><b id='bPageNum'></b></td>
						<td>&nbsp;</td>
					</tr>	
					<tr valign="top">
						<td><b>Ảnh minh họa</b></td>
						<td>
							<label>Chọn ảnh khác</label><br />
							<input type="file" id="fImage" size="40px" name="fImage" onblur="checkImage()"/><br>
							<span>Chấp nhận file .gif, .jpg, .jpeg, .png (tối đa 3Mb)</span><br />
							<b id='bfImage'></b>							
						</td>
						<td>&nbsp;</td>
					</tr>
					<tr valign="top">
						<td><b>Mô tả</b></td>
						<td>
							<textarea name="txtDesc" cols="40" rows="10" onkeyup="count()" onkeydown="checkcount()" onblur="checkDesc()"><?php echo $desc;?></textarea><br />
							<span id=idCount style="text-align: right;">Tối đa 2000 ký tự</span><br />
							<b id='bDesc'></b><br /><br />
						</td>	
						<td><b style="color:#8C0000">*</b></td>			
					</tr>
				</table>	
			</td>
			</tr>		
			</table>	
			
			<hr width='600px' />
			<div align="center">
				<input type="button" id='btnSave' value="Lưu" onclick="saveEbook()"/>&nbsp;&nbsp;&nbsp;
				<input type='button' id='btnResset' value='Hủy' <?php echo "onclick='editEbook({$id})'"; ?> />
			</div>						
			<br /><br />
			</form>
			
			<?php
				/* free result set */
				$result->close();
				$conn->close();
			}
			else
			{
				echo "<div align='center'>Ebook này không tồn tại.</div>";
			}
			?>
			
		</div>
	</div>
	<?php require "../components/footer.php" ?>
</div>
</body>
</html>
